package com.CompSci.Game;
import java.awt.Canvas;



public class Main extends Canvas implements Runnable
  {
  public static final long serialVersionUID = 1L;
  
  public static int width = 675;
	public static int height = width / 9 * 8;
	public static int scale = 1;
  
  private Thread thread;
	private JFrame frame;
  private boolean running = false;
  
  private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  
  private static BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
  private static Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg,  new Point(0, 0), "blank cursor");
  
  public Main()
  {
    Dimension size = new Dimension(width * scale, height * scale);
		setPreferredSize(size);
		
		screen = new Screen(width, height);
		frame = new JFrame();
  }
  
  public synchronized void start()
  {
    running = true;
    thread = new Thread(this, "Display");
    thread.start();
  }
  
  public synchronized void stop()
  {
    running = false;
		try
		{
			thread.join();
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
  }
  
  public void run()
  {
    long lastTime = System.nanoTime();
		long timer = System.currentTimeMillis();
		final double ns = 1000000000.0 / 60.0;
		double delta = 0;
		int updates = 0, frames = 0;
		requestFocus();
		while (running)
		{
			long now = System.nanoTime();
			delta += (now - lastTime) / ns;
			lastTime = now;
			while(delta >= 1)
			{
				update();
				--delta;
				++updates;
			}
			
			render();
			++frames;
			
			if(System.currentTimeMillis() - timer > 1000)
			{
				timer += 1000;
				frame.setTitle(updates + "ups, " + frames + " fps");
				frames = 0;
				updates = 0;
			}
		}
		stop();
  }
  
  private void update()
  {
  }
  
  private void render()
  {
    BufferStrategy bs = getBufferStrategy();
    if(bs == null)
    {
      createBufferStrategy(3);
      return;
    }
    
    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    g.dispose();
    bs.show();
  }
  
  public static void main(String...args)
  {
    Main game = new Main();
		game.frame.setResizable(false);
		game.frame.setTitle(Game.title);
		game.frame.add(game);
		game.frame.pack();
		game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		game.frame.setLocationRelativeTo(null);
		game.frame.getContentPane().setCursor(blankCursor);
		game.frame.setVisible(true);
		
		
		game.start();
  }
}